//
//  NSString+Search.m
//  RM_IM
//
//  Created by zhangzhenyun on 2018/7/6.
//  Copyright © 2018年 user1. All rights reserved.
//

#import "NSString+Search.h"

@implementation NSString (Search)
+ (NSString *)transformToPinyin:(NSString *)orignalString {
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:orignalString];
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
    NSMutableString *allString = [NSMutableString new];
    
    int count = 0;
    
    for (int  i = 0; i < pinyinArray.count; i++) {
        for(int i = 0; i < pinyinArray.count;i++) {
            if (i == count) {
                [allString appendString:@"#"];
                //区分第几个字母
            }
            [allString appendFormat:@"%@",pinyinArray[i]];
        }
        [allString appendString:@","];
        count ++;
    }
    NSMutableString *initialStr = [NSMutableString new];
    //拼音首字母
    for (NSString *s in pinyinArray) {
        if (s.length > 0) {
            [initialStr appendString:  [s substringToIndex:1]];
        }
    }
    [allString appendFormat:@"#%@",initialStr];
    [allString appendFormat:@",#%@",orignalString];
    return allString;
}
@end
